<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package routines
* @subpackage routines.models.behaviors
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: hidable.php 160 2010-10-21 14:29:03Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/models/behaviors/hidable.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class HidableBehavior extends ModelBehavior {

var $_defaults = array(
	'hideField' => 'disabled',
	'hideValue' => 1,
	'notHideValue' => 0
);

var $settings = array();

function setup(&$Model, $settings = array()) {
	$this->settings[$Model->alias] = $settings;
}

function _getSettings(&$Model, $settings = array()) {
	$settings = Set::merge($this->settings[$Model->alias], $settings);
	if (isset($settings['noUseModelVars']) && $settings['noUseModelVars']) {
		return Set::merge($this->_defaults, $settings);
	} else {
		return Set::merge($this->_defaults, array_intersect_key(get_object_vars($Model), $this->_defaults), $settings);
	}
}

function _idExists(&$Model, $id) {
	$Model->id = $id;
	return $Model->exists();
}

function isHidden(&$Model, $id, $validate = TRUE) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if ($validate) {
		if (!$Model->hasField($hideField) || !$this->_idExists($Model, $id)) {
			return NULL;
		}
	}
	$data = $Model->find('first', array('conditions' => array($Model->escapeField($Model->primaryKey) => $id), 'fields' => array($Model->escapeField($hideField)), 'recursive' => -1, 'callbacks' => FALSE));
	if (isset($data[$Model->alias][$hideField])) {
		return ($data[$Model->alias][$hideField] == $hideValue);
	} else {
		return NULL;
	}
}

function hide(&$Model, $id, $validate = TRUE) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if ($validate) {
		if (!$Model->hasField($hideField) || !$this->_idExists($Model, $id)) {
			return FALSE;
		}
	}
	return (bool)$Model->save(array($Model->alias => array($Model->primaryKey => $id, $hideField => $hideValue)), array('validate' => FALSE, 'callbacks' => FALSE));
}

function show(&$Model, $id, $validate = TRUE) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if ($validate) {
		if (!$Model->hasField($hideField) || !$this->_idExists($Model, $id)) {
			return FALSE;
		}
	}
	return (bool)$Model->save(array($Model->alias => array($Model->primaryKey => $id, $hideField => $notHideValue)), array('validate' => FALSE, 'callbacks' => FALSE));
}

function toggleVisibility(&$Model, $id, $validate = TRUE) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if ($validate) {
		if (!$Model->hasField($hideField) || !$this->_idExists($Model, $id)) {
			return FALSE;
		}
	}
	if (($isHidden = $this->isHidden($Model, $id, FALSE)) === TRUE) {
		return $this->show($Model, $id, FALSE);
	} elseif ($isHidden === FALSE) {
		return $this->hide($Model, $id, FALSE);
	} else {
		return FALSE;
	}
}

function beforeFind(&$Model, $query) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if ($Model->hasField($hideField) && !(bool)Configure::read('Config.noHidable')) {
		$query['conditions'][$Model->escapeField($hideField) . ' <>'] = $hideValue;
	}
	return $query;
}

}


?>